using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CommandPatternApp
{
    public partial class FormMain : Form
    {
        TextEditor _editor;
        CmdFactory _factory;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            // Created editor and factory.
            _editor = new TextEditor(this._textBox);
            _factory = new CmdFactory(_editor);

            // Get concrete command objects from factory and assign to corresponding menu 
            // items and tool strip buttons.
            this._menuFileOpen.Command = _factory.OpenFileCmd();
            this._menuFileSave.Command = _factory.SaveFileCmd();
            this._menuEditUndo.Command = this._editor.UndoCmd = _factory.UndoCmd(this._menuEditUndo);
            this._menuEditCut.Command = _factory.CutCmd();
            this._menuEditCopy.Command = _factory.CopyCmd();
            this._menuEditPaste.Command = _factory.PasteCmd();
            this._buttonOpen.Command = _factory.OpenFileCmd();
            this._buttonSave.Command = _factory.SaveFileCmd();
            this._buttonCopy.Command = _factory.CopyCmd();
            this._buttonPaste.Command = _factory.PasteCmd();

            // Start timer to let time event handler enable/disable Copy and Paste toolstrip buttons.
            this._timer.Start();
        }

        private void menuFileExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Resize the text box when the form is resized.
        private void FormMain_Resize(object sender, EventArgs e)
        {
            this._textBox.Location = new Point(0, this._toolStrip.Height);
            this._textBox.Width = this.ClientRectangle.Width;
            this._textBox.Height = this.ClientRectangle.Height - this._toolStrip.Height;
        }

        // Determine if Copy button and Paste button should be enabled or disabled.
        private void OnTimer(object sender, EventArgs e)
        {
            // Enable Copy button if there is selected text in text box, otherwise disable it.
            this._buttonCopy.Enabled = this._textBox.SelectedText.Length > 0;
            // Enable Paste button if clip boad contains text, otherwise disable it.
            this._buttonPaste.Enabled = Clipboard.ContainsText();
        }

        // Determin if Copy, Cut and Paste menu should be enabled or disabled when Edit menu is clicked.
        private void menuEdit_Popup(object sender, EventArgs e)
        {
            this._menuEditCut.Enabled = this._textBox.SelectedText.Length > 0;
            this._menuEditCopy.Enabled = this._textBox.SelectedText.Length > 0;
            this._menuEditPaste.Enabled = Clipboard.ContainsText();
        }
    }
}